<template>
    <view class="page-box">
        <header class="header">
            <view class="progress">
                <view class="progress-step column rowCenter verCenter" :class="{active: index <= curr}" v-for="(item,index) in progress" :key="index">
                    <text class="status-bar"></text>
                    <text class="tt">{{item}}</text>
                </view>
            </view>
        </header>
        <section class="section">
            <view class="speed-of-progress">
                <view class="box">
                    <view class="dd-text" v-for="(item,index) in logs" :key="index">
                        <text class="arrow"></text>
                        <view class="text">{{item.create_name}} {{item.log_data.message}}</view>
                        <view class="time">{{item.create_time}}</view>
                    </view>
                </view>
            </view>
        </section>
    </view>
</template>

<script>
    import { API } from '@/util/api.js';
    export default {
        data() {
            return {
                self_business_progress: '', //自主业务进度
                self_business_progress_val: '',
                self_business_progress_node: '', //自主业务进度节点
                self_business_progress_node_val: '',
                curr: 0,
                progress: ['预审', '审批', '签约', '车务', '请款'],
                detail: {},
                logs: []
            };
        },
        onLoad(options) {
            this.order_id = options.order_id;
        },
        onShow() {
            this.getOrderDetail();
            this.getLogs();
        },
        methods: {
            /**
             * 获取业务进度
             */
            getOrderDetail() {
                this.request(API.orderDetail, 'GET', { order_id: this.order_id }, true).then(res => {
                    if (res.code === 0) {
                        this.detail = res.data;
                        this.self_business_progress = res.data.self_business_progress;
                        this.self_business_progress_node = res.data.self_business_progress_node;
                        this.self_business_progress_val = res.data.self_business_progress_val;
                        this.self_business_progress_node_val = res.data.self_business_progress_node_val;

                        const progressVal = res.data.self_business_progress_val;
                        var progressIndex = this.progress.indexOf(progressVal);
                        if (progressVal == '结算') {
                            progressIndex = 4;
                        }

                        if (progressIndex !== -1) {
                            this.curr = progressIndex;
                        }

                        uni.setNavigationBarTitle({
                            title: res.data.product_name + ' - 订单进度'
                        });

                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });
                    }
                });
            },
            /**
             * 获取日志
             */
            getLogs() {
                this.request(API.getLogs, 'GET', { obj_type: 401, obj_id: this.order_id }, true).then(res => {
                    if (res.code === 0) {
                        const uniqueMessages = new Set();
                        this.logs = res.data.list.filter(log => {
                            if (!uniqueMessages.has(log.log_data.message)) {
                                uniqueMessages.add(log.log_data.message);
                                return true;
                            }
                            return false;
                        });
                    } else {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });
                    }
                });
            }
        }
    };
</script>

<style scoped lang="scss">
    @import '@/assets/css/order/selfDetail.scss';
</style>